package com.foryang.springcloud.controller;

import com.foryang.springcloud.entities.CommonResult;
import com.foryang.springcloud.entity.Payment;
import com.foryang.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        if(payment != null){
            return new CommonResult(200,"查询成功,port:" + serverPort, payment);
        }else{
            return new CommonResult(444,"没有对应记录，查询ID:" + id, null);
        }
    }

    @GetMapping("/payment/findAll")
    public CommonResult<List<Payment>> findAll(){
        List<Payment> list = paymentService.findAll();
        if(list.size() > 0){
            return new CommonResult(200,"查询成功,port:" + serverPort, list);
        }else{
            return new CommonResult(444,"没有对应记录，查询ID:" , null);
        }
    }

    @PostMapping("/payment/create")
    public CommonResult<Payment> create(Payment payment){
        Payment payment1 = paymentService.create(payment);
        if(payment != null){
            return new CommonResult(200,"插入成功,port:" + serverPort, payment1);
        }else{
            return new CommonResult(444,"插入失败", null);
        }
    }

    @PostMapping("/payment/update")
    public CommonResult<Payment> update(Payment payment){
        Object object = paymentService.updateById(payment);
        if(object != null){
            return new CommonResult(200,"修改成功,port:" + serverPort, object);
        }else{
            return new CommonResult(444,"修改失败", null);
        }
    }

    @GetMapping("/payment/delete/{id}")
    public CommonResult<Payment> delete(@PathVariable Long id){
        Object object = paymentService.deleteById(id);
        if(object != null){
            return new CommonResult(200,"删除成功,port:" + serverPort, object);
        }else{
            return new CommonResult(444,"删除失败", null);
        }
    }



}
